#include "requestmapper.h"


RequestMapper::RequestMapper(QObject* parent)
    : HttpRequestHandler(parent)
{
    // empty

    opinfController=new OpinfController(this);
    //关联HTTP POST的自定义槽函数
    connect(opinfController, SIGNAL(sendOpinfMain(QString,QString)), this, SLOT(receiveOpinfData(QString,QString)));

    boxNumberController=new BoxNumberController(this);
    //关联HTTP POST的自定义槽函数
    connect(boxNumberController, SIGNAL(sendBoxfMain(QString,QString)), this, SLOT(receiveBoxData(QString,QString)));

}

void RequestMapper::service(HttpRequest& request, HttpResponse& response)
{
    QByteArray path=request.getPath();
    qDebug("RequestMapper: path=%s",path.data());

    QByteArray Method;
    Method=request.getMethod();
    qDebug()<<__LINE__<<"getMethod="<<Method;//POST GET 等访问方式

    if (path=="/")
    {
//        helloWorldController.service(request, response);
    }
    else if (path=="/Box_number")
    {
        //GET获取药箱编号
        response.setStatus(200);
        boxNumberController->service(request, response);

    }
    else if (path=="/opinf")
    {
        //发送病人手术信息
        response.setStatus(200);
        opinfController->service(request, response);
    }
    else if (path=="/over_light")
    {
        if(Method == "POST")
        {
            response.setStatus(200);
            opinfController->overLight(request, response);
        }

    }
    else if (path=="/bottle_state")
    {
        if(Method == "POST")
        {
            response.setStatus(200);
            opinfController->bottleState(request, response);
        }

    }
    else
    {
        response.setStatus(404,"Not found");
        response.write("The URL is wrong, no such document.");
    }

    qDebug("RequestMapper: finished request");
}

//关联HTTP POST的自定义槽函数
void RequestMapper::receiveOpinfData(QString serial, QString data)
{
    qDebug()<<__LINE__<<"serial="<<serial<<"data="<<data;
    emit sendRequesfMain(serial,data);
}

//关联HTTP GET的自定义槽函数
void RequestMapper::receiveBoxData(QString serial, QString data)
{
    qDebug()<<__LINE__<<"serial="<<serial<<"data="<<data;
    emit sendRequesfMain(serial,data);
}

